package com.opensky.qatest.ui.page;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.InvitePageForm;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * 
 * OpenSky - "Invite page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class InvitePage extends LoadableComponent<InvitePage> implements IPage {

    // ************************ Locators ************************

    private static final String INVITE_PAGE_URL = Properties.get("env.root.url") + "welcome/invite";

    private static final String EARN_CREDIT_STEP = "//div[@class='steps']//li[@class='active']/span[text()='Earn Credit']";

    private static final String START_SHOPPING_BTN = "//div[@class='start-shopping']//a[@class='button']";

    private static final String INVALID_EMAIL_ALERT_CONTAINER = "//div[@id='error-message']";

    private static final String INVALID_EMAIL_ALERT_TEXT = "There was an error submitting the email invitation form.";

    private static final String INVALID_EMAIL_LABEL = "//label[@class='errors' and contains(., 'is not a valid email address')]";

    private static final String SUCCESS_SEND_MESSAGE = "//div[@id='notification']//li[text()='Invitations were sent to: %s']";

    // ************************ Complex page objects **************************

    private InvitePageForm inviteForm;

    private final WebDriverWrapper driver;

    public InvitePage(WebDriverWrapper driver) {
	this.driver = driver;
	inviteForm = new InvitePageForm(driver);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(INVITE_PAGE_URL);
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(INVITE_PAGE_URL, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(INVITE_PAGE_URL);
    }

    public boolean isBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("EARN_CREDIT_STEP", By.xpath(EARN_CREDIT_STEP)),
		driver.waitAndVerifyElementPresent("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN)));
    }

    public boolean isInvalidEmailAlertPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementWithTextPresent("INVALID_EMAIL_ALERT_CONTAINER",
		By.xpath(INVALID_EMAIL_ALERT_CONTAINER), INVALID_EMAIL_ALERT_TEXT), driver.waitAndVerifyElementPresent(
		"INVALID_EMAIL_LABEL", By.xpath(INVALID_EMAIL_LABEL)));
    }

    public boolean isSuccessSendMessagePresent(List<String> toEmails) {

	String emailsText = "";
	for (String toEmail : toEmails) {
	    emailsText += toEmail + ", ";
	}
	emailsText = StringUtils.removeEnd(emailsText, ", ");

	String message = String.format(SUCCESS_SEND_MESSAGE, emailsText);

	return driver.waitAndVerifyElementPresent("SUCCESS_SEND_MESSAGE", By.xpath(message));
    }

    // ************************ GET/SET methods **************************

    public InvitePageForm getInviteForm() {
	return inviteForm.isComponentLoaded() ? inviteForm : null;
    }

    public SalesPage clickStartShoppingBtn() {

	SalesPage salesPage = new SalesPage(driver);
	driver.waitAndClick("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN));
	return salesPage.isPageLoaded() ? salesPage : null;
    }

    @Override
    public String getName() {
	return "Invite page";
    }
}
